#pragma once
#pragma message "HI3863 Hardware fastpin added"
#include "gpio.h"
#include "pinctrl.h"
#include "iot_gpio.h"
#include "hal_gpio.h"
#include "platform_core_rom.h"
#include "soc_osal.h"

FASTLED_NAMESPACE_BEGIN

#define SPI_DATA CONFIG_SPI_DO_MASTER_PIN
#define SPI_CLOCK CONFIG_SPI_CLK_MASTER_PIN

template<uint8_t PIN, uint32_t MASK> class _HI3863PIN {
public:
  typedef volatile uint32_t * port_ptr_t;
  typedef uint32_t port_t;

  inline static void setOutput() {
      //IoTGpioSetDir(PIN, IOT_GPIO_DIR_OUT);
      ////printf("\r\n fastled hi3863 pin setOutput start PIN=%d\r\n",PIN);
      uapi_gpio_set_dir((pin_t)PIN, GPIO_DIRECTION_OUTPUT);
  }

  inline static void setInput() {
      //IoTGpioSetDir(PIN, IOT_GPIO_DIR_IN);
      //printf("\r\n fastled hi3863 pin setInput start PIN=%d\r\n",PIN);
      uapi_gpio_set_dir((pin_t)PIN, GPIO_DIRECTION_OUTPUT);
  }

  inline static void hi() __attribute__ ((always_inline)) {
      //IoTGpioSetOutputVal(PIN, IOT_GPIO_VALUE1);
      
      //printf("\r\n fastled hi3863 pin hi start PIN=%d\r\n",PIN);
      uapi_gpio_set_val((pin_t)PIN, GPIO_LEVEL_HIGH);
  }

  inline static void lo() __attribute__ ((always_inline)) {
      //IoTGpioSetOutputVal(PIN, IOT_GPIO_VALUE0);
      
      printf("\r\n fastled hi3863 pin lo start PIN=%d\r\n",PIN);
      uapi_gpio_set_val((pin_t)PIN,  GPIO_LEVEL_LOW);
  }

  inline static void set(FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) {
      //IoTGpioSetOutputVal(PIN, (IotGpioValue)val);
      //printf("\r\n fastled hi3863 pin set start PIN=%d val=%d\r\n",PIN,val);
      if(val ==1){
        uapi_gpio_set_val((pin_t)PIN,  GPIO_LEVEL_HIGH);
      }else{
        uapi_gpio_set_val((pin_t)PIN,  GPIO_LEVEL_LOW);
      }
  }

  inline static void strobe() __attribute__ ((always_inline)) { toggle(); toggle(); }

  inline static void toggle() __attribute__ ((always_inline)) {
    //printf("\r\n fastled hi3863 pin toggle start PIN=%d\r\n",PIN);
    uapi_gpio_toggle((pin_t)PIN);
  }

  inline static void hi(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { hi(); }
  inline static void lo(FASTLED_REGISTER port_ptr_t port) __attribute__ ((always_inline)) { lo(); }
  inline static void fastset(FASTLED_REGISTER port_ptr_t port, FASTLED_REGISTER port_t val) __attribute__ ((always_inline)) { 
    set(val);
  }

  inline static port_t hival() __attribute__ ((always_inline)) { return 0; }
  inline static port_t loval() __attribute__ ((always_inline)) { return 0; }
  inline static port_ptr_t port() __attribute__ ((always_inline)) { return NULL; }
  inline static port_t mask() __attribute__ ((always_inline)) { return 0; }
};


#define _FL_DEFPIN(PIN) template <> class FastPin<PIN> : public _HI3863PIN<PIN, ((uint32_t)1 << (PIN % 32))> {};

// Define all possible pins.  If the pin is not valid for a particular HI3863 variant, the pin number
// will be shifted into the 192-255 range, in effect rendering it unusable.
_FL_DEFPIN( 0); _FL_DEFPIN( 1); _FL_DEFPIN( 2); _FL_DEFPIN( 3);
_FL_DEFPIN( 4); _FL_DEFPIN( 5); _FL_DEFPIN( 6); _FL_DEFPIN( 7);
_FL_DEFPIN( 8); _FL_DEFPIN( 9); _FL_DEFPIN(10); _FL_DEFPIN(11);
_FL_DEFPIN(12); _FL_DEFPIN(13); _FL_DEFPIN(14);

#define HAS_HARDWARE_PIN_SUPPORT 1

FASTLED_NAMESPACE_END
